import Schema from 'validate'
const formInfo = new Schema(
  {
    point: {
      type: String,
      required: true,
      message: '小区名不能为空'
    },
    building: {
      type: String,
      required: true,
      message: '栋数不能为空'
    },
    room: {
      type: String,
      required: true,
      message: '房间号不能为空'
    },
    name: {
      type: String,
      required: true,
      message: '姓名不能为空'
    },
    gender: {
      type: Number,
      required: true,
      message: '房间号不能为空'
    },
    mobile: {
      type: String,
      required: true,
      match: /^1[3-9]\d{9}$/,
      message: '手机号错误'
    },
    idcardFrontUrl: {
      type: String,
      required: true,
      message: '人像面不能为空'
    },
    idcardBackUrl: {
      type: String,
      required: true,
      message: '国徽面不能为空'
    }
  },
  { strict: false }
)
Page({
  data: {
    id: '',
    point: '',
    building: '',
    room: '',
    // 业主信息
    name: '',
    // 性别
    gender: 0,
    // 手机号
    mobile: '',
    // 人面像
    idcardFrontUrl: '',
    // 国徽相
    idcardBackUrl: '',
    // 房屋状态
    status: 0
  },
  async onLoad ({ point, buildings, room, id }) {
    if (id) {
      wx.setNavigationBarTitle({ title: '编辑房屋信息' })
      const res = await wx.http({ url: `/room/${id}` })
      this.setData({ ...res.data })
    } else {
      this.setData({
        point,
        building: buildings,
        room
      })
    }
  },
  // 上传图片点击事件
  chooseMedia (e) {
    console.log(e.mark.type)
    // 本地图片转换临时地址
    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      // 转换返回信息
      success: res => {
        // console.log(res.tempFiles[0].tempFilePath)
        // 临时地址上传
        wx.uploadFile({
          // 接口地址(完整的)
          url: wx.http.baseURL + '/upload', //仅为示例，非真实的接口地址
          // 上传的图片临时地址
          filePath: res.tempFiles[0].tempFilePath,
          name: 'file',
          // 上传携带内容
          header: { Authorization: `Bearer ${getApp().token}` },
          // 返回数据
          success: res => {
            // console.log(res)
            // 返回的是字符串，使用JSON.parse转换数组
            const url = JSON.parse(res.data)
            // console.log(url)
            // if (e.mark.type === 'idcardFrontUrl') {
            //   this.setData({
            //     idcardFrontUrl: url.data.url
            //   })
            // } else {
            //   this.setData({
            //     idcardBackUrl: url.data.url
            //   })
            // }
            this.setData({
              [e.mark.type]: url.data.url
            })
          }
        })
      }
    })
  },
  // 删除
  removePicture (e) {
    // 移除图片的类型（身份证正面或反面）
    const type = e.mark.type
    this.setData({ [type]: '' })
  },
  // 提交审核
  async goList () {
    const { point, building, room, name, gender, mobile, idcardFrontUrl, idcardBackUrl } = this.data
    const data = { point, building, room, name, gender, mobile, idcardFrontUrl, idcardBackUrl }
    // 校验正则
    const errors = formInfo.validate(data)
    // 判断正则是否通过
    if (errors.length) {
      // 没通过弹窗提示
      wx.utils.toast(errors[0].message)
      return
    }
    // 判读是否有id
    if (this.data.id) {
      console.log(this.data.id)
      data.id = this.data.id
    }
    const res = await wx.http({ url: '/room', method: 'post', data })
    console.log(res)
    wx.showToast({ title: '操作成功', icon: 'success' })

    setTimeout(() => {
      wx.navigateTo({ url: '/house_pkg/pages/list/index' })
    }, 1500)
  }
})
